package com.example.scenictourback.controller;

import com.example.scenictourback.common.Result;
import com.example.scenictourback.dto.ScenicAreaDTO;
import com.example.scenictourback.entity.ScenicArea;
import com.example.scenictourback.service.ScenicAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/scenic")
public class ScenicAreaController {
    
    @Autowired
    private ScenicAreaService scenicAreaService;
    
    /**
     * 获取所有景区列表（管理员权限）
     */
    @GetMapping("/list/all")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<?> listAllScenicAreas() {
        List<ScenicAreaDTO> scenicAreas = scenicAreaService.listAllScenicAreas();
        return Result.success(scenicAreas);
    }
    
    /**
     * 获取所有开放的景区列表
     */
    @GetMapping("/list")
    public Result<?> listOpenScenicAreas() {
        List<ScenicAreaDTO> scenicAreas = scenicAreaService.listOpenScenicAreas();
        return Result.success(scenicAreas);
    }
    
    /**
     * 根据ID获取景区详情
     */
    @GetMapping("/{id}")
    public Result<?> getScenicAreaById(@PathVariable Integer id) {
        ScenicAreaDTO scenicArea = scenicAreaService.getScenicAreaById(id);
        return Result.success(scenicArea);
    }
    
    /**
     * 根据名称搜索景区
     */
    @GetMapping("/search")
    public Result<?> searchScenicAreasByName(@RequestParam String name) {
        List<ScenicAreaDTO> scenicAreas = scenicAreaService.searchScenicAreasByName(name);
        return Result.success(scenicAreas);
    }
    
    /**
     * 新增景区（管理员权限）
     */
    @PostMapping("/add")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<?> addScenicArea(@RequestBody ScenicArea scenicArea) {
        ScenicAreaDTO savedScenicArea = scenicAreaService.addScenicArea(scenicArea);
        return Result.success("添加成功", savedScenicArea);
    }
    
    /**
     * 更新景区信息（管理员权限）
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<?> updateScenicArea(@PathVariable Integer id, @RequestBody ScenicArea scenicArea) {
        ScenicAreaDTO updatedScenicArea = scenicAreaService.updateScenicArea(id, scenicArea);
        return Result.success("更新成功", updatedScenicArea);
    }
    
    /**
     * 删除景区（管理员权限）
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<?> deleteScenicArea(@PathVariable Integer id) {
        scenicAreaService.deleteScenicArea(id);
        return Result.success("删除成功");
    }
} 